Class PngLoader Extends CalLoader'Base Class for Loading

	Field pos:Int
	Field cdata:DataBuffer
	
	Field chunkSize:Int,section:String', short, text, _i, _j, _ref, colors, delayDen, delayNum, frame, i, index, key,  
	Field width:Int, height:Int,bits:Int,colortype:Int,compressionMethod:Int,filterMethod:Int,interlaceMethod:Int
	Field ppux:Int,ppuy:Int,unit:Int,compressionMethodb:Int,compressedicc:Int,compressionflag:Int,cbits:int
	'Field pallette:[]
	Field imgData:int[]
	'Field transparency
	'Field text
	
	Method Load:Object(data:String)
		
		If loadedobject = Null Then Return Null
		If DataBuffer(loadedobject) = Null Then Return Null
		cdata = DataBuffer(loadedobject)
		Print "Loading PNG Data"
		
		pos = 8
		While pos < cdata.Length()
			If pos > cdata.Length() Then 
				loaded = True
				Return Null
			Endif
			chunkSize = cdata.PeekInt(pos)
			pos = pos + 4
			Print "ChunkSize: "+chunkSize
			section = cdata.PeekString(pos,4)
			pos = pos + 4
			Print "Section: "+section
			
			Select section
			
				Case "IHDR"
					width = cdata.PeekInt(pos)
					pos = pos + 4
					height = cdata.PeekInt(pos)
					pos = pos + 4
					Print "Width: "+width+" Height: "+height
					bits = cdata.PeekByte(pos)
					pos = pos + 1
					colortype = cdata.PeekByte(pos)
					pos = pos + 1
					compressionMethod = cdata.PeekByte(pos)
					pos = pos + 1
					filterMethod = cdata.PeekByte(pos)
					pos = pos + 1
					interlaceMethod = cdata.PeekByte(pos)
					pos = pos + 1

				
				Case "pHYs"
					ppux = cdata.PeekInt(pos)
					pos = pos + 4
					ppuy = cdata.PeekInt(pos)
					pos = pos + 4
					unit = cdata.PeekByte(pos)
					pos = pos + 1
				
				Case "IDAT","fdAT"
					If section = "fdAT" Then
						pos = pos + 4
						chunkSize = chunkSize - 4
						imgData = New Int[width*height]
						For Local i:Int = 0 To (width*height)-1
							imgData[i] = cdata.PeekByte(pos)
							pos = pos + 1
						Next
						
					Endif
				
					
				
				
				Case "iCCP"
					Local iccp:String
					Local scount:int
					For Local i:Int = 0 To 78
						scount = scount + 1 
						If cdata.PeekByte(pos+i) = 0 Then Exit
					Next
					iccp = cdata.PeekString(pos,scount)
					pos = pos + scount
					Print "ICCP: "+iccp
					compressionMethodb = cdata.PeekByte(pos)
					Print "Compression: "+compressionMethodb
					pos = pos + 1
					If compressionMethodb = 0 then
					
						compressionflag = cdata.PeekByte(pos)
						pos = pos + 1
						cbits = cdata.PeekByte(pos)
						pos = pos + 1
						Print "CFlag: "+compressionflag+" Bits: "+cbits
						
						
					
					Endif
					'compressedicc = cdata.PeekByte(pos)
					'pos = pos + 1
					
				Default	
					loaded = True
					Return Null
					
			End Select
			
			pos = pos + 4
			
			
		Wend
		
		loaded = True
		Print "Return!"
		Return loadedobject
		
		
	End
	
	Method IsLoaded:Bool()
		Return loaded
	End
	
	Method GetLoadedObject:Object()
		Return loadedobject
	End
	

	

End


Class CalLoadDataBufferCallback Extends CalLoadingCallback Implements IOnLoadDataComplete

		
	

	Method OnLoadDataComplete:Void( datab:DataBuffer, path:String )
		finished = True
		data = path
		loader.loadedobject = Object(datab)
		
	End
	
	
	Method Load:Void(type:Int=0,url:String,data:String="",mime:String="text/plain;charset=utf-8",enc:String="utf8")
		fileurl = url
		Print "Loading: "+url
		
		DataBuffer.LoadAsync( url,Self )
		
		
	End	
	

End Class